/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PanelConsultaGeneral.java
 *
 * Created on 29/05/2011, 03:48:10 PM
 */
package gui;
import controlador.*;
import java.util.*;
import logica.*;
import javax.swing.*;
/**
 *
 * @author Gamboa Family
 */
public class PanelConsultaGeneral extends javax.swing.JPanel {
    PanelResultadosConsulta panelResultados;
    Boolean puedeDescargar =false;
    String codigoUsuarioConsulta;

    /** Creates new form PanelConsultaGeneral */
    public PanelConsultaGeneral() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanelBotonesAtrasAdelante = new javax.swing.JPanel();
        jBAtras = new javax.swing.JButton();
        jBAdelante = new javax.swing.JButton();
        jPanelCentral = new javax.swing.JPanel();
        jPanelInicial = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jTFEntrada = new javax.swing.JTextField();
        jBBuscar = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        setBackground(new java.awt.Color(255, 255, 255));
        setBorder(null);

        jPanelBotonesAtrasAdelante.setBackground(new java.awt.Color(255, 255, 255));

        jBAtras.setBackground(new java.awt.Color(255, 255, 255));
        jBAtras.setFont(new java.awt.Font("Verdana", 1, 18));
        jBAtras.setForeground(new java.awt.Color(235, 30, 30));
        jBAtras.setText("<");
        jBAtras.setEnabled(false);
        jBAtras.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBAtrasActionPerformed(evt);
            }
        });

        jBAdelante.setBackground(new java.awt.Color(255, 255, 255));
        jBAdelante.setFont(new java.awt.Font("Verdana", 1, 18));
        jBAdelante.setForeground(new java.awt.Color(235, 30, 30));
        jBAdelante.setText(">");
        jBAdelante.setEnabled(false);
        jBAdelante.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBAdelanteActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanelBotonesAtrasAdelanteLayout = new javax.swing.GroupLayout(jPanelBotonesAtrasAdelante);
        jPanelBotonesAtrasAdelante.setLayout(jPanelBotonesAtrasAdelanteLayout);
        jPanelBotonesAtrasAdelanteLayout.setHorizontalGroup(
            jPanelBotonesAtrasAdelanteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelBotonesAtrasAdelanteLayout.createSequentialGroup()
                .addComponent(jBAtras)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jBAdelante)
                .addContainerGap(913, Short.MAX_VALUE))
        );
        jPanelBotonesAtrasAdelanteLayout.setVerticalGroup(
            jPanelBotonesAtrasAdelanteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelBotonesAtrasAdelanteLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jBAtras, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jBAdelante, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        jPanelCentral.setBackground(new java.awt.Color(255, 255, 255));

        jPanelInicial.setBackground(new java.awt.Color(255, 255, 255));

        jLabel2.setBackground(new java.awt.Color(255, 255, 255));
        jLabel2.setFont(new java.awt.Font("Verdana", 1, 24));
        jLabel2.setForeground(new java.awt.Color(130, 134, 135));
        jLabel2.setText("Entrada");

        jTFEntrada.setFont(new java.awt.Font("Verdana", 0, 18));
        jTFEntrada.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTFEntradaActionPerformed(evt);
            }
        });

        jBBuscar.setBackground(new java.awt.Color(255, 255, 255));
        jBBuscar.setFont(new java.awt.Font("Verdana", 1, 18));
        jBBuscar.setForeground(new java.awt.Color(235, 30, 30));
        jBBuscar.setText("Buscar");
        jBBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBBuscarActionPerformed(evt);
            }
        });

        jLabel1.setBackground(new java.awt.Color(255, 255, 255));
        jLabel1.setFont(new java.awt.Font("Verdana", 1, 48));
        jLabel1.setForeground(new java.awt.Color(235, 30, 30));
        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconos/consultaGeneral.jpg"))); // NOI18N
        jLabel1.setText("Consulta General");

        javax.swing.GroupLayout jPanelInicialLayout = new javax.swing.GroupLayout(jPanelInicial);
        jPanelInicial.setLayout(jPanelInicialLayout);
        jPanelInicialLayout.setHorizontalGroup(
            jPanelInicialLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelInicialLayout.createSequentialGroup()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTFEntrada, javax.swing.GroupLayout.DEFAULT_SIZE, 752, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jBBuscar)
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelInicialLayout.createSequentialGroup()
                .addContainerGap(171, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addGap(168, 168, 168))
        );
        jPanelInicialLayout.setVerticalGroup(
            jPanelInicialLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelInicialLayout.createSequentialGroup()
                .addContainerGap(59, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanelInicialLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTFEntrada, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jBBuscar)
                    .addComponent(jLabel2))
                .addContainerGap())
        );

        javax.swing.GroupLayout jPanelCentralLayout = new javax.swing.GroupLayout(jPanelCentral);
        jPanelCentral.setLayout(jPanelCentralLayout);
        jPanelCentralLayout.setHorizontalGroup(
            jPanelCentralLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelCentralLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanelInicial, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanelCentralLayout.setVerticalGroup(
            jPanelCentralLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelCentralLayout.createSequentialGroup()
                .addGap(41, 41, 41)
                .addComponent(jPanelInicial, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(175, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanelBotonesAtrasAdelante, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addContainerGap())
                    .addComponent(jPanelCentral, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanelBotonesAtrasAdelante, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanelCentral, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jBBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBBuscarActionPerformed
        // TODO add your handling code here:
        realizarBusqueda();
    }//GEN-LAST:event_jBBuscarActionPerformed

    private void jBAtrasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBAtrasActionPerformed
        // TODO add your handling code here:        
        jPanelCentral.removeAll();
        jPanelCentral.add(jPanelInicial);
        jBAdelante.setEnabled(true);
        jBAtras.setEnabled(false);
        validate();
        repaint();
    }//GEN-LAST:event_jBAtrasActionPerformed

    private void jBAdelanteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBAdelanteActionPerformed
        // TODO add your handling code here:
        jPanelCentral.removeAll();
        jPanelCentral.add(panelResultados);
        jBAtras.setEnabled(true);
        jBAdelante.setEnabled(false);
        validate();
        repaint();
    }//GEN-LAST:event_jBAdelanteActionPerformed

    private void jTFEntradaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTFEntradaActionPerformed
        // TODO add your handling code here:
        realizarBusqueda();
    }//GEN-LAST:event_jTFEntradaActionPerformed

    public void realizarBusqueda(){
        ControladorMaterial controlMat = new ControladorMaterial();
        panelResultados = new PanelResultadosConsulta();
        if(puedeDescargar)panelResultados.habilitarDescarga();
        panelResultados.setUsuarioQueConsulta(codigoUsuarioConsulta);
        //el metodo consultaGeneral() retorna un Vector de Material para que la interfaz funcione y muestre 
        //los resultados de la busqeda.
        Vector<Material> resultadosBusqueda = controlMat.consultaGeneral(jTFEntrada.getText());
        if(resultadosBusqueda != null && !resultadosBusqueda.isEmpty()){
            jPanelCentral.removeAll();            
            panelResultados.mostrarResulatdos(resultadosBusqueda, jTFEntrada);            
            jPanelCentral.add(panelResultados);
            jBAtras.setEnabled(true);
            validate();
            repaint();   
        }
        else{
            JOptionPane.showMessageDialog(this, "No se encontró ningún resultado "
                    + "para su consulta.\nPor favor intente con otras palabras.", 
                        "Sin resultados", JOptionPane.ERROR_MESSAGE);
            jTFEntrada.setText("");
        }
    }

    public void habilitarDescarga(){
        puedeDescargar = true;
    }

    public void setUsuarioQueConsulta(String codigoUsr){
        codigoUsuarioConsulta = codigoUsr;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBAdelante;
    private javax.swing.JButton jBAtras;
    private javax.swing.JButton jBBuscar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanelBotonesAtrasAdelante;
    private javax.swing.JPanel jPanelCentral;
    private javax.swing.JPanel jPanelInicial;
    private javax.swing.JTextField jTFEntrada;
    // End of variables declaration//GEN-END:variables
}
